<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 用户公告类
 */
class Comment extends MY_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('comment_mdl');
    }

    public function index(){
        //分页
        $url = '';
        //$this->db->start_cache();

        $this->load->library('pagination');
        $this->config->load('page', TRUE);
        $config = $this->config->config;
        $config = $config['page'];
        $config['base_url'] = site_url('comment/index').$url;
        $config['total_rows'] = $this->comment_mdl->get_count();

        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();

        $per_page = $this->input->get('per_page');
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->order_by('update_time desc');
        $data['list'] = $this->comment_mdl->select();
        $data['rows'] = $config['total_rows'];
        //echo $this->db->last_query();
        //$this->db->flush_cache();

        $this->_view('index',$data);
    }

    /**
     * 新增
     */
    public function set(){
        if($_SESSION['user']['group'] == 3 ||$_SESSION['user']['group'] == 4){
            show_404();
        }
        if($this->input->is_ajax_request()){
            //加载表单验证
            $this->load->library('form_validation');
            $this->form_validation->set_error_delimiters('<span class="am-icon-close am-icon-sm">  ', '</span><br/>');

            $this->form_validation->set_rules($this->comment_mdl->_validation);

            if($this->form_validation->run() == FALSE){
                echo validation_errors();
            }else{
                $post = $this->input->post();
                $res = $this->comment_mdl->add($post);
                echo $res === false ? '添加失败！':'1';
            }
        }else{
            $this->_view('set');
        }
    }

    /**
     * 删除
     */
    public function delete(){
        // if(!$this->input->is_ajax_request()){
        //     show_404();
        // }
        if($_SESSION['user']['group'] == 3){
            show_404();
        }

        $get = $this->input->get('cid');
        $this->db->where('id',$get);
        $res = $this->comment_mdl->delete();
        echo $res === false ? '删除失败！': redirect(site_url('comment/index'));
    }

     /**
     * 全选删除
     */
    public function del(){
        // if(!$this->input->is_ajax_request()){
        //     show_404();
        // }
        if($_SESSION['user']['group'] == 3){
            show_404();
        }

        $get = $this->input->get('id');
        $get = explode(",",$get);
        //print_r($get);exit;
        for($i=0;$i<count($get);$i++){
            $this->db->where('id',$get[$i]);
            $res = $this->comment_mdl->delete();
        }
        
        echo $res === false ? '删除失败！': 1;
    }

    /**
     * 修改
     */
    public function update(){
        if($_SESSION['user']['group'] == 3){
            show_404();
        }

        $cid = $this->input->get('cid');
        if(empty($cid) || !is_numeric($cid)){
            show_error('参数错误！');
        }

        $this->db->where('id',$cid);
        $res = $this->comment_mdl->get_one();

        if(empty($res)){
            show_error('参数错误！');
        }

        $this->_view('update',['res'=>$res]);

    }

    /**
     * 修改
     */
    public function update_env(){
        if(!$this->input->is_ajax_request()){
            show_404();
        }

        $post = $this->input->post();

        //判断用户是否更改标题
        $this->db->where('id',$post['cid']);
        $this->db->select('title');
        $res_r = $this->comment_mdl->get_one();

        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<span class="am-icon-close am-icon-sm">  ', '</span><br/>');

        $this->form_validation->set_rules($this->comment_mdl->_validation);

        if($post['title'] == $res_r['title']){
            $data = $post;
            $data['title'] = $post['title'].$_SERVER['REQUEST_TIME'];
            $this->form_validation->set_data($data);
        }

        if($this->form_validation->run() == FALSE){
            echo validation_errors();
        }else{
            $res = $this->comment_mdl->edit($post);
            echo $res === false ? '添加失败！':'1';
        }

    }

    /**
     * 查看公告详情
     */
    public function show(){
        $cid = $this->input->get('cid');
        $this->db->where('id',intval($cid));
        $res = $this->comment_mdl->get_one();

        if(empty($res['id'])){
            show_error('该公告不存在');
        }
        $this->_view('show',['res'=>$res]);
    }

}

/* End of file Comment.php */
/* Location: ./application/controllers/Comment.php */